﻿package  
{
	import flash.display.*;
	import flash.display.DisplayObject;
	import flash.display.IBitmapDrawable;
	import flash.events.AsyncErrorEvent;
	import flash.events.NetStatusEvent;
	import flash.events.SecurityErrorEvent;
	import flash.media.Video;
	import flash.net.NetConnection;
	import flash.net.NetStream;
	
	/**
	 * ...
	 * @author ...
	 */
	public class VideoSlide extends Slide
	{
		protected var m_video:Video;
        private var m_connection:NetConnection;
        private var m_stream:NetStream;
		private var m_finished:Boolean;
		public function VideoSlide(  url:String, description:String ) 
		{
			m_requiresPreLoad = false;
			m_video = null;
			m_finished = false;

			super( url, description );

            m_connection = new NetConnection();
            m_connection.addEventListener(NetStatusEvent.NET_STATUS, netStatusHandler);
            //m_connection.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
            m_connection.connect(null);
			this.addEventListener(Slide.FADE_IN_COMPLETE, startPlaying );
		}
		
		private function netStatusHandler(event:NetStatusEvent):void 
		{
            switch (event.info.code) {
                case "NetConnection.Connect.Success":
                    connectStream();
                    break;
                case "NetStream.Play.StreamNotFound":
                    trace("Unable to locate video: " + m_url);
                    break;
				case "NetStream.Play.Stop":
					trace("Finished playing video");
					m_finished = true;
					break;
            }
        }
		
        private function connectStream():void {
            m_stream = new NetStream(m_connection);
            m_stream.addEventListener(NetStatusEvent.NET_STATUS, netStatusHandler);
            //m_stream.addEventListener(AsyncErrorEvent.ASYNC_ERROR, asyncErrorHandler);
            m_video.attachNetStream(m_stream);
        }

		private function startPlaying():void
		{
            m_stream.play(m_url);
		}
		protected override function loadBmp(content:Object):void
		{
			m_video = new Video( this.width, this.height );
			m_video.smoothing = true;
			//m_video.pixelSnapping = PixelSnapping.NEVER;
		}

		protected override function unloadBmp():void
		{
			// todo. dispose of all this crap.
		}
		
		protected override function get bmp():DisplayObject
		{
			return m_video;
		}
		
		public override function get finished():Boolean
		{
			return m_finished;
		}
	}
	
}